package com.lauroc.dept.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lauroc.basic.utils.AjaxResult;
import com.lauroc.basic.utils.PageList;
import com.lauroc.basic.utils.TreeUtil;
import com.lauroc.dept.domain.Department;
import com.lauroc.dept.mapper.DepartmentMapper;
import com.lauroc.dept.query.DepartmentQuery;
import com.lauroc.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper mapper;

    @Override
    public Department getById(Integer id) {

        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AjaxResult getAll() {

        return AjaxResult.createSuccess(mapper.selectAll());
    }

    @Override
    public void saveAndUpdate(Department department) {
        if (department.getId()==null){
            mapper.insertSelective(department);
            createSaveOrUpdate(department);
        }else {
            //获取新增中的父id的数组id

            createSaveOrUpdate(department);
        }
    }

    private void createSaveOrUpdate(Department department) {
        //获取新增中的父id的数组id
        Long[] parentIds = department.getParentIds();
        StringBuilder sb = new StringBuilder();
        if (parentIds != null && parentIds.length > 0) {
            //通过将获取的数组进行长度-1获取最后一个数的父id
            Long parentId = parentIds[parentIds.length - 1];
            //将这个id插入到父id中
            department.setParentId(parentId);
            for (Long id : parentIds
            ) {
                //拼接字符串
                sb.append("/").append(id);
            }
            //将增加进去的数据的自己的id加进去
            sb.append("/").append(department.getId());
        } else {
            department.setParentId(null);
            sb.append("/").append(department.getId());
        }
        department.setDirPath(sb.toString());

        mapper.updateByPrimaryKey(department);
    }

    @Override
    public void delete(Long id) {

        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageList<Department> findPage(DepartmentQuery department) {

        //1.分页插件
        PageHelper.startPage(department.getPage(), department.getSize());
        //2.查询所有的部门信息
        List<Department> pageList = mapper.findPage(department);
        //3.封装PageInfo
        PageInfo<Department> pageInfo = new PageInfo<>(pageList);

        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<Department> findParentDepts() {
        return mapper.findParentDepts();
    }

    @Override
    public List<Department> treeDepts() {
        //1.查询所有的部门信息
        List<Department> departments = mapper.selectAll();
        //2.调用工具类中的方法
        List<Department> tree = TreeUtil.getTree(departments);
        return tree;
    }

    /**
     * 批量删除
     * @param
     */
    @Override
    public void batchDelete(Long[] ids) {
        mapper.batchDelete(ids);
    }
}
